import { ICPU } from "./api/cpu";
import { IInterrupt } from "./api/interrupt";
import { IPPU } from "./api/ppu";
import { CPUBus } from "./bus/cpu-bus";
import { PPUBus } from "./bus/ppu-bus";
import { Cartridge } from "./cartridge/cartridge"
import { CPU } from "./cpu/cpu";
import { DMA } from "./dma/dma";
import { Interrupt } from "./interrupt/interrupt";
import { PPU } from "./ppu/ppu";
import { RAM } from "./ram/ram";

function testCart(rom: Uint8Array) {
    let sram = new Uint8Array(0);
    let cart = new Cartridge(rom, sram)
    console.log("cart", cart);
}

function testCpuBus(rom: Uint8Array) {

}

function testCpu(rom: Uint8Array) {

}

function testPPU(rom: Uint8Array) {
    let ppu: IPPU;
    let cpu: ICPU;
    let cpuInter: IInterrupt;

    const ppuRam = new RAM(1024 * 2, 0x2000); // 0x2000 ~ 0x2800
    const cpuRam = new RAM(1024 * 2, 0); // 0x0000 ~ 0x0800
    const backgroundPalette = new RAM(16, 0x3F00); // 0x3F00 ~ 0x3F10
    const spritePalette = new RAM(16, 0x3F10); // 0x3F10 ~ 0x3F20

    let sram = new Uint8Array(0);
    let cart = new Cartridge(rom, sram)
    let dma = new DMA();

    //cpu的读写
    let cpuBus = new CPUBus(cart, cpuRam, new DMA());
    cpu = new CPU(cpuBus);
    cpuInter = new Interrupt(cpu)

    // ppu的读写
    let ppuBus = new PPUBus(cart, ppuRam, backgroundPalette, spritePalette)
    ppu = new PPU(
        (data: Uint8Array) => {
            console.log("ccc", data);
        },
        ppuBus,
        cart.mapper,
        cpuInter
    )

    cpuBus.ppu = ppu
    // 
    dma.cpu = cpu;
    dma.ppu = ppu;

    console.log("dma", dma)
    console.log("cpu", cpu)
    console.log("ppu", ppu)

    // 时钟
    function frame() {
        cpu.clock()

        ppu.clock()
        ppu.clock()
        ppu.clock()

        requestAnimationFrame(frame)
    }

    requestAnimationFrame(frame)
}


export {
    testCart,
    testCpuBus,
    testCpu,
    testPPU
}